﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VisualCardPrinter_NPPrinter_New
{
    public class VisualCardPrinter_NPPrinter
    {
        private int _ComPort;
        int _Bandrate;
        byte[] _printer;

        int _x;
        int _y;
        int _direction;
        byte[] _fontname;
        int _fontsize;
        int com;
        public VisualCardPrinter_NPPrinter(int ComPort, int Bandrate, byte[] printer, int x, int y, int direction, byte[] fontname, int fontsize, ref int ret)
        {
            this._ComPort = ComPort;
            this._Bandrate = Bandrate;
            this._printer = printer;
            this._x = x;
            this._y = y;
            this._direction = direction;
            this._fontname = fontname;
            this._fontsize = fontsize;
        }

        public VisualCardPrinter_NPPrinter(int ComPort, int Bandrate)
        {
            this._ComPort = ComPort;
            this._Bandrate = Bandrate;
        }

        //进卡 0、端口打开失败  1、进卡失败   2、成功进卡
        public int CardInOver() {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var no = RCP.CardInOver(x, 1);
                if (no != 32)
                {
                    //MessageBox.Show("进卡失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                RCP.CommClose(x);
                return 2;
                //textBox5.Text = no.ToString();
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
            RCP.CommClose(x);
        }
        //============================================================================
        //出卡  0、端口打开失败 1、出卡失败  2、出卡成功
        public int Dischange() {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var no = RCP.Dischange(x, 1, 1);
                if (no != 32)
                {
                    //MessageBox.Show("出卡失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                //textBox5.Text = no.ToString();
                RCP.CommClose(x);
                return 2;
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
            //RCP.CommClose(x);
        }
        //============================================================================
        //发送文本  0、端口打开失败  1、发送文本失败  2、发送文本成功
        public int PrintCharacterDataSetting(string print) {
            int x = _x;
            int y = _y;
            int m_Printmode = 0;
            string text = "";
            int textlength = 0;
            try
            {
                x = Convert.ToInt16(_x);
                y = Convert.ToInt16(_y);
                m_Printmode = Convert.ToInt16(_direction);
                text = print;
                textlength = RCP.修正字符长度(text);

            }
            catch (Exception)
            {

            }
            if (m_Printmode > 3 || m_Printmode < 0)
            {
                //MessageBox.Show("纵/横向的值：只能在0-3之间！");
            }
            else
            {
                var m_hCom = RCP.CommOpen(_ComPort, _Bandrate);
                if (m_hCom != 0)
                {
                    StringBuilder strb = new StringBuilder(text);
                    var no = RCP.PrintCharacterDataSetting(m_hCom, m_Printmode, x, y, strb, textlength, 1);
                    if (no != 32)
                    {
                        //MessageBox.Show("发送失败~！");
                        RCP.CommClose(m_hCom);
                        return 1;
                    }
                    RCP.CommClose(m_hCom);
                    return 2;
                    //textBox5.Text = no.ToString();
                }
                else
                {
                    //MessageBox.Show("串口打开失败~！");
                    return 0;
                }
                //RCP.CommClose(m_hCom);
            }
            return 0;
        
        }
        //===========================================================================
        //仅擦除  0、端口打开失败  1、仅擦除失败  2、仅擦除成功
        public int ErasePrintDischarge2()
        {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var no = RCP.ErasePrintDischarge(x, 1, 1, 0, 1);
                if (no != 32)
                {
                    //MessageBox.Show("擦除失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                //textBox5.Text = no.ToString();
                RCP.CommClose(x);
                return 2;
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
           
        }
        //===========================================================================
        //擦除并打印  0、端口打开失败  1、擦除并打印失败  2、擦除并打印成功
        public int ErasePrintDischarge3()
        {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var no = RCP.ErasePrintDischarge(x, 1, 1, 1, 1);
                if (no != 32)
                {
                    //MessageBox.Show("打印失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                //textBox5.Text = no.ToString();
                RCP.CommClose(x);
                return 2;
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
           
        }
        //===========================================================================
        //清除缓存  0、端口打开失败  1、清除缓存失败  2、清除缓存成功
        public int PrintBufferClear()
        {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var no = RCP.PrintBufferClear(x, 1);
                if (no != 32)
                {
                    //MessageBox.Show("清理缓存失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                //textBox5.Text = no.ToString();
                RCP.CommClose(x);
                return 2;
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
        }
        //===========================================================================
        //发送图像  0、端口打开失败  1、发送图像失败  2、发送图像成功

        public int PrintImageDataSetting_byfile(string path) { 
            int x = 0;
            int y = 0;
            string text = "";
            try
            {
                x = Convert.ToInt16("横坐标");
                y = Convert.ToInt16("竖坐标");
                text = path;

            }
            catch (Exception)
            {

            }
            var m_hCom = RCP.CommOpen(_ComPort, _Bandrate);
            if (m_hCom != 0)
            {
                StringBuilder strb = new StringBuilder(text);
                var no = RCP.PrintImageDataSetting_byfile(m_hCom, x, y, strb, 1);
                if (no != 32)
                {
                    //MessageBox.Show("发送失败~！");
                    RCP.CommClose(m_hCom);
                    return 1;
                }
                else
                {
                    //MessageBox.Show("发送成功~！");
                    RCP.CommClose(m_hCom);
                    return 2;
                }
                //textBox5.Text = no.ToString();
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return 0;
            }
         
   
        }
        //===========================================================================
        //读卡并读取扇区 0、打开端口失败  1、读取失败 2、读取成功
        public string RfidCardRequestAndReadData(string shanqu,string pwd)
        {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var sqh = Convert.ToByte(shanqu);
                var password = new StringBuilder(pwd);
                byte[] 字符 = new byte[48];
                var no = RCP.RfidCardRequestAndReadData(x, sqh, 0, password, 字符,1);
                if (no != 32)
                {
                    //MessageBox.Show("读数据失败~！");
                    RCP.CommClose(x);
                    return "1";
                }
                else
                {
                    //textBox8.Text = Encoding.Default.GetString(字符);
                    RCP.CommClose(x);
                    return Encoding.Default.GetString(字符);
                }
                //textBox3.Text = no.ToString();
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return "0";
            }
            
        
        }

        //===========================================================================
        //写卡 0、打开端口失败  1、写卡失败 2、写卡成功
        public int RfidCardRequestAndWriteData(string shanqu,string pwd ,string str)
        {

            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                var sqh = Convert.ToByte(shanqu);
                var password = new StringBuilder(pwd);
                StringBuilder 字符 = new StringBuilder(str);
                var no = RCP.RfidCardRequestAndWriteData(x, sqh, 0, password, 字符, 1);
                if (no != 32)
                {
                    //MessageBox.Show("写数据失败~！");
                    RCP.CommClose(x);
                    return 1;
                }
                //textBox3.Text = no.ToString();
                RCP.CommClose(x);
                return 2;
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                
                return 0;
            }
        }

        //===========================================================================
        //读序列号 0、打开端口失败  1、读序列号失败 2、读序列号成功
        public string RfidCardRequestAndReadNum()
        {
            var x = RCP.CommOpen(_ComPort, _Bandrate);
            if (x != 0)
            {
                byte[] 字符 = new byte[16];
                var no = RCP.RfidCardRequestAndReadNum(x, 字符, 1);
                if (no != 32)
                {
                    //MessageBox.Show("读卡序列号失败~！");
                    RCP.CommClose(x);
                    return "1";
                }
                else
                {
                  //  textBox5.Text = Encoding.Default.GetString(字符); ;
                    RCP.CommClose(x);

                    string temp = Encoding.Default.GetString(字符).Replace("\0","") ;
                    string tempInt = Convert.ToInt64(temp, 16).ToString();
                    return tempInt;
                }
                //textBox3.Text = no.ToString();
            }
            else
            {
                //MessageBox.Show("串口打开失败~！");
                return "0";
            }
            
        }



        
        //===========================================================================
        //读序列号 0、打开端口失败  1、读序列号失败 2、读序列号成功
        //public unsafe int _stdcallRequestStatus()
        //{
        //    var x = RCP.CommOpen(_ComPort, _Bandrate);
        //    if (x != 0)
        //    {
        //        //char* Res = '1';
        //        int no = RCP._stdcallRequestStatus(_ComPort, Res, 10);
        //        return no;
        //        //if (no != 32)
        //        //{
        //        //    //MessageBox.Show("读卡序列号失败~！");
        //        //    RCP.CommClose(x);
        //        //    return "读卡失败！";
        //        //}
        //        //else
        //        //{
        //        //    //  textBox5.Text = Encoding.Default.GetString(字符); ;
        //        //    RCP.CommClose(x);
        //        //    //return Encoding.Default.GetString(字符);
        //        //}
        //        //textBox3.Text = no.ToString();
        //    }
        //    else
        //    {
        //        //MessageBox.Show("串口打开失败~！");
        //        //return "端口打开失败！";
        //        return 0;
        //    }
        
        
        //}
        //卡加密
        public string ZSGW_KeyPass(string cardID)
        {
            //卡号固定10位数字,密钥固定12位
            if (cardID == "1")
            {
                return "2";
            }
            //第一步后的值
            string Hex = "";
            //第二步后的值
            string TwoHex = "";
            //第三步后的值
            string ThreeHex = "";
            //第四步后的值
            string FourHex = "";


            //第一步 除去 2,4,7 位数字
            for (int i = 0; i < cardID.Length; i++)
            {
                if (i != 2 && i != 4 && i != 7)
                {
                    Hex += cardID[i];
                }
            }


            //第二部 每一位乘3
            for (int i = 0; i < Hex.Length; i++)
            {
                string num = Hex[i].ToString();
                string numTwo = (Convert.ToInt32(num) * 3).ToString();
                if (numTwo.Length < 2)
                {
                    numTwo = "0" + numTwo;
                }
                TwoHex += numTwo;
            }


            //第三部 将下标为6到8的数字删掉
            ThreeHex = TwoHex.Remove(6, 2);



            //第四部 将每一步的数都加5然后进行16进制转换
            for (int i = 0; i < ThreeHex.Length; i++)
            {
                string num = ThreeHex[i].ToString();
                int numTwo = Convert.ToInt32(num) + 5;
                FourHex += Convert.ToString(numTwo, 16);
            }

            //return FourHex;
            return "FFFFFFFFFFFF";
        }




        


          
    }
}
